<script lang="ts" setup>
import { ref, computed } from 'vue'
import { usePhoneStore, useMemberStore } from '@/stores'
import { PageSkip } from '@/utils/tools'
import { getRankingsListApi } from '@/services/eat'
import { onLoad } from '@dcloudio/uni-app'

const phoneStore = usePhoneStore()
const memberStore = useMemberStore()

const query = defineProps<{
  marketingId: string
}>()

const navBarHight = computed(() => {
  return phoneStore.phoneInfo!.bottom - phoneStore.phoneInfo!.top
})
/** 滚动到底部触发 */
const onScrolltolower = () => {
  console.log('滚动到底部了')
}

onLoad(() => {
  getRankingsList()
})
type listType = {
  crown?: string
  clientId?: number | string
  nickName?: string
  avatar?: string
  totalCount?: number
  _index?: number
}

const topList = ref<listType[]>([
  {
    crown: '/static/images/rankingsPage/11.png',
  },
  {
    crown: '/static/images/rankingsPage/22.png',
  },
  {
    crown: '/static/images/rankingsPage/33.png',
  },
])
const list = ref<listType[]>()
const userInfo = ref<listType>()
/** 获取排行榜列表 */
const getRankingsList = async () => {
  try {
    const res = await getRankingsListApi({ marketingId: query.marketingId })
    console.log(res)
    if (res.code === 200) {
      // console.log(res.data?.slice(0, 3))
      const processedData = res.data?.slice(0, 3)
      topList.value = topList.value?.map((item: listType, index: number) => {
        return processedData[index]
          ? {
              ...(processedData[index] ?? {}),
              ...item,
              avatar: processedData[index].avatar || '/static/images/logo.svg',
            }
          : { ...item }
      })
      list.value = res.data?.slice(3)
      userInfo.value = res.data?.find(
        (item: listType) => item.clientId === memberStore.userInfo?.id,
      )
      if (userInfo.value) {
        const _index =
          res.data.findIndex((item: listType) => item.clientId === memberStore.userInfo?.id) + 1

        userInfo.value = { ...userInfo.value, _index }
      }

      console.log('userInfo', userInfo.value)
    }
    // 排序
    const item3 = topList.value[2]
    topList.value.splice(2, 1)
    topList.value.unshift(item3)
  } catch (error) {
    console.log(error)
  }
}
</script>
<template>
  <view class="rankingsPage">
    <!-- 顶部排行榜 -->
    <view class="_rankingsTop">
      <view
        class="navBar"
        :style="{
          top: Number(phoneStore.phoneInfo?.top) + 'px',
          height: `${navBarHight}px`,
          lineHeight: `${navBarHight}px`,
        }"
      >
        <view @tap="PageSkip('navigateBack')" class="iconfont icon_base_left"></view>
        <view>排行榜</view>
        <view style="width: 50rpx"></view>
      </view>
      <view class="_rankingsTop_img">
        <view class="topImg" v-for="item in topList" :key="item.crown">
          <img class="crown" :src="item.crown" />
          <img
            class="avatar"
            :src="
              item.avatar ||
              'h'
            "
          />
          <view
            class="nickName"
            :style="{
              color: item.clientId === memberStore.userInfo?.id ? '#8ADF4B' : '#000000',
            }"
            >{{ item.nickName || '暂无上榜' }}</view
          >
          <view class="count">{{ item.totalCount || '—' }}</view>
        </view>
      </view>
    </view>
    <!-- 列表 -->
    <scroll-view
      scroll-y
      @scrolltolower="onScrolltolower"
      style="height: calc(100vh - 530rpx)"
      class="_rankingsList"
    >
      <view class="_rankingsItem" v-for="(item, index) in list" :key="item.clientId">
        <view
          class="_sort"
          :style="{ color: item.clientId === memberStore.userInfo?.id ? '#8ADF4B' : '#999999' }"
          >{{ index + 4 }}</view
        >
        <img class="_img" :src="item.avatar || '/static/images/logo.png'" />
        <view class="_nickNameBox">
          <view
            class="_nickName"
            :style="{ color: item.clientId === memberStore.userInfo?.id ? '#8ADF4B' : '#000000' }"
            >{{ item.nickName }}</view
          >
          <view class="_countBox">
            <view
              class="_count"
              :style="{ color: item.clientId === memberStore.userInfo?.id ? '#8ADF4B' : '#858585' }"
              >累计邀请人数：{{ item.totalCount }}</view
            >
          </view>
        </view>
      </view>
    </scroll-view>
    <!-- 定位 -->
    <!-- <view class="userLocation">
      <view class="_sort">{{ userInfo?._index || '—' }}</view>
      <img class="_img" :src="memberStore.userInfo?.headImg || '/static/images/logo.svg'" />
      <view class="_nickName">{{ memberStore.userInfo?.nickName }}</view>
      <view class="_countBox">
        <view class="_count">{{ userInfo?.totalCount || 0 }}</view>
        <view class="_text">累计邀请人数</view>
      </view>
    </view> -->
  </view>
</template>
<style lang="scss" scoped>
._rankingsTop {
  position: relative;
  width: 100%;
  height: 570rpx;
  background-color: $uni-color-motif;
  .navBar {
    position: absolute;
    width: 100%;
    top: 50px;
    left: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: transparent;
    color: #000;
    padding: 0 0 0 20rpx;
    .icon_base_left {
      width: 50rpx;
      min-height: 20rpx;
    }
  }
  ._rankingsTop_img {
    width: 100%;
    display: flex;
    justify-content: space-between;
    padding: 276rpx 35rpx 0rpx;
    .topImg {
      width: 200rpx;
      display: flex;
      flex-direction: column;
      align-items: center;
      font-size: 24rpx;
      color: #999999;
      position: relative;
      .crown {
        width: 158rpx;
        height: 154rpx;
        position: absolute;
        top: -22rpx;
        left: 26rpx;
      }
      .avatar {
        width: 124rpx;
        height: 124rpx;
        border-radius: 200rpx;
      }
      .nickName {
        max-width: 200rpx;
        height: 40rpx;
        line-height: 40rpx;
        margin: 20rpx 0 8rpx;
        font-size: 30rpx;
        color: #000;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .count {
        font-size: 28rpx;
        line-height: 40rpx;
        color: #000;
      }
    }
    .topImg:nth-child(2) {
      min-height: 204rpx;
      transform: translateY(-30rpx);
      .crown {
        width: 186rpx;
        height: 180rpx;
        top: -22rpx;
        left: 14rpx;
      }
      .avatar {
        width: 156rpx;
        height: 156rpx;
      }
    }
  }
}
._rankingsList {
  background-color: #f8f8f8;
  border-radius: 32rpx 32rpx 0px 0px;
  margin-top: -40rpx;
  position: relative;
  z-index: 10;
  padding: 30rpx 30rpx 0;
}
._rankingsItem,
.userLocation {
  width: 100%;
  height: 200rpx;
  display: flex;
  align-items: center;
  padding-left: 60rpx;
  font-size: 28rpx;
  background: #ffffff;
  margin-bottom: 20rpx;
  border-radius: 40rpx;
}
.userLocation {
  position: fixed;
  left: 0;
  bottom: 0;
  background: #fff;
  box-shadow: 0px 6rpx 13rpx 16rpx rgba(0, 0, 0, 0.04);
  border-radius: 32rpx 32rpx 0px 0px;
}

._sort {
  color: #b1c0cd;
  font-size: 60rpx;
  width: 40rpx;
  margin-right: 40rpx;
  text-align: left;
  font-weight: bold;
}
._img {
  width: 120rpx;
  height: 120rpx;
  border-radius: 200rpx;
}
._nickNameBox {
  margin-left: 40rpx;
}
._nickName {
  flex: 1;
  text-align: left;
  color: #333333;
  font-size: 32rpx;
  font-weight: bold;
  line-height: 44rpx;
}
._countBox {
  display: flex;
  align-items: center;
  line-height: 60rpx;
  ._count {
    font-family: PingFang SC-Light;
    font-size: 28rpx;
    color: #858585;
  }
}
</style>
